package com.chenfeng.ego.rest.service.impl;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.chenfeng.ego.commons.pojo.EgoNav;
import com.chenfeng.ego.commons.pojo.EgoNavFinalResult;
import com.chenfeng.ego.mapper.TbItemCatMapper;
import com.chenfeng.ego.mapper.TbItemMapper;
import com.chenfeng.ego.pojo.TbItem;
import com.chenfeng.ego.pojo.TbItemCat;
import com.chenfeng.ego.pojo.TbItemCatExample;
import com.chenfeng.ego.pojo.TbItemExample;
import com.chenfeng.ego.rest.service.TbItemCatService;
@Service
public class TbItemCatServiceImpl implements TbItemCatService
{

	@Resource
	TbItemCatMapper itemCatMapper;
	@Override
	//商品左侧菜单的显示
	public EgoNavFinalResult finaMenu()
	{
		List<Object> list = selByParam(0);
		EgoNavFinalResult enf = new EgoNavFinalResult();
		enf.setData(list);
		return enf;
	}
	@Override
	public List<Object> selByParam(long id)
	{
		//其实这可树跟递归文件一样的
		TbItemCatExample example = new TbItemCatExample();
		example.createCriteria().andParentIdEqualTo(id);
		//查找到id为0的list集合
		List<TbItemCat> iteam = itemCatMapper.selectByExample(example);
		//最终需要的list集合
		List<Object> listNav = new ArrayList<>();
		//循环id为0的list集合
		for (TbItemCat tbItemCat : iteam)
		{
			//判断是否是叶子节点如果是叶子节点则直接else如果不是叶子节点则进行递归操作
			if(tbItemCat.getIsParent())
			{
				EgoNav ego = new EgoNav();
				ego.setU("/products/"+tbItemCat.getId()+".html");
				ego.setN("<a href='/products/"+tbItemCat.getId()+".html'>"+tbItemCat.getName()+"</a>");
				ego.setI(selByParam(tbItemCat.getId()));
				//将父节点添加到需要的集合中
				listNav.add(ego);
			}else
			{
				//最终的叶子节点添加到集合中
				listNav.add("/products/"+tbItemCat.getId()+"|"+tbItemCat.getName());
			}		
		}
		return listNav;
	}
}
